package com.xce.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.UUID;

/**
 * @ClassName OneTocken
 * @Description TODO
 * @Author lld
 * @Date 2025/7/8 3:16
 * @Version 1.0
 */
@Component
public class OneTocken  extends AbstractNameValueGatewayFilterFactory {
    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return (exchange, chain) -> {
            // 写法跟全局一样了 then是下一步做什么
            return chain.filter(exchange).then(
                    // 异步线程
                    Mono.fromRunnable(() -> {
                        // 逻辑处理
                        HttpHeaders headers = exchange.getResponse().getHeaders();
                        // equalsIgnoreCase()方法用于将此字符串与指定的对象比较，不考虑大小写。
                        if (config.getValue().equalsIgnoreCase("jwt")) {
                            headers.add(config.getName(), "jwt");
                        } else if (config.getValue().equalsIgnoreCase("uuid")) {
                            headers.add(config.getName(),UUID.randomUUID().toString());

                        }
                    })
            );
        };
    }
}
